//
//  UIViewController+XJHud.m
//  XJProgressHUD
//
//  Created by 石显军 on 2019/12/16.
//  Copyright © 2019 石显军. All rights reserved.
//

#import "UIViewController+XJHud.h"
#import <objc/runtime.h>

@implementation UIViewController (XJHud)

- (void)setXj_hud:(JGProgressHUD *)xj_hud
{
    objc_setAssociatedObject(self, @selector(setXj_hud:), xj_hud, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}

- (JGProgressHUD *)xj_hud
{
    return objc_getAssociatedObject(self, @selector(setXj_hud:));
}

/**
 Show the loading
 */
- (void)xj_showLoading
{
    [self xj_showLoadingMessage:nil];
}

- (void)xj_showLoadingMessage:(NSString *)message
{
    [self xj_showLoadingMessage:message inView:self.view];
}

- (void)xj_showLoadingMessage:(NSString *)message inView:(UIView *)view
{
    if (self.xj_hud == nil)
    {
        self.xj_hud = [JGProgressHUD progressHUDWithStyle:JGProgressHUDStyleDark];
        self.xj_hud.textLabel.text = message;
        [self.xj_hud showInView:view];
        return;
    }
    
    if (self.xj_hud.isVisible)
    {
        self.xj_hud.textLabel.text = message;
    }
    else
    {
        [self.xj_hud showInView:view];
    }
}

/**
 Dismisses the loading animated.
 */
- (void)xj_dismiss
{
    [self xj_dismissAnimated:YES];
}
- (void)xj_dismissAnimated:(BOOL)animated
{
    [self xj_dismissAfterDelay:0 animated:YES];
}
- (void)xj_dismissAfterDelay:(NSTimeInterval)delay
{
    [self xj_dismissAfterDelay:delay animated:YES];
}
- (void)xj_dismissAfterDelay:(NSTimeInterval)delay animated:(BOOL)animated
{
    if (self.xj_hud)
    {
        [self.xj_hud dismissAfterDelay:delay animated:animated];
        self.xj_hud = nil;
    }
    else
    {
        [self xj_removeAllHUDInView];
    }
}

- (void)xj_removeAllHUDInView
{
    NSArray *array = [JGProgressHUD allProgressHUDsInView:self.view];
    for (JGProgressHUD *hud in array)
    {
        [hud dismiss];
    }
}

/**
 Show the Progress
 */
- (void)xj_showProgress
{
    [self xj_showProgressMessage:nil];
}

- (void)xj_showProgressMessage:(NSString * _Nullable)message
{
    [self xj_showProgressMessage:message inView:self.view];
}

- (void)xj_showProgressMessage:(NSString * _Nullable)message inView:(UIView *)view
{
    self.xj_hud = [JGProgressHUD progressHUDWithStyle:JGProgressHUDStyleDark];
    self.xj_hud.indicatorView = [[JGProgressHUDPieIndicatorView alloc] init];
    self.xj_hud.textLabel.text = message;
    self.xj_hud.progress = 0.f;
    [self.xj_hud showInView:self.view];
}

- (void)xj_updateProgressWithValue:(CGFloat)progressValue
{
    if (self.xj_hud) {
        self.xj_hud.progress = progressValue;
    }
}

/**
 show error message
 */
- (void)xj_showErrorMessage:(NSString *)message
{
    [self xj_showErrorMessage:message inView:self.view];
}
- (void)xj_showErrorMessage:(NSString *)message inView:(UIView *)view
{
    JGProgressHUD *HUD = [JGProgressHUD progressHUDWithStyle:JGProgressHUDStyleDark];
    HUD.textLabel.text = message;
    HUD.indicatorView = [[JGProgressHUDErrorIndicatorView alloc] init];
    [HUD showInView:view];
    [HUD dismissAfterDelay:1.5f];
}

/**
 show success message
 */
- (void)xj_showSuccessMessage:(NSString *)message
{
    [self xj_showSuccessMessage:message inView:self.view];
}

- (void)xj_showSuccessMessage:(NSString *)message inView:(UIView *)view
{
    [self xj_dismiss];
    
    JGProgressHUD *HUD = [JGProgressHUD progressHUDWithStyle:JGProgressHUDStyleDark];
    HUD.textLabel.text = message;
    HUD.indicatorView = [[JGProgressHUDSuccessIndicatorView alloc] init];
    [HUD showInView:view];
    [HUD dismissAfterDelay:1.5f];
}

/**
 show bottom center message
 */
- (void)xj_showBottomMessage:(NSString *)message
{
    [self xj_showBottomMessage:message inView:self.view];
}
- (void)xj_showBottomMessage:(NSString *)message inView:(UIView *)view
{
    [self xj_dismiss];
    
    JGProgressHUD *HUD = [JGProgressHUD progressHUDWithStyle:JGProgressHUDStyleDark];
    HUD.indicatorView = nil;
    HUD.textLabel.text = message;
    HUD.position = JGProgressHUDPositionBottomCenter;
    [HUD showInView:view];
    [HUD dismissAfterDelay:1.5f];
}

/**
 show center message
 */
- (void)xj_showCenterMessage:(NSString *)message
{
    [self xj_showCenterMessage:message inView:self.view];
}

- (void)xj_showCenterMessage:(NSString *)message inView:(UIView *)view
{
    [self xj_dismiss];
    
    JGProgressHUD *HUD = [JGProgressHUD progressHUDWithStyle:JGProgressHUDStyleDark];
    HUD.indicatorView = nil;
    HUD.textLabel.text = message;
    HUD.position = JGProgressHUDPositionCenter;
    [HUD showInView:view];
    [HUD dismissAfterDelay:1.5f];
}


/**
 show top center message
 */
- (void)xj_showTopMessage:(NSString *)message
{
    [self xj_showTopMessage:message inView:self.view];
}
- (void)xj_showTopMessage:(NSString *)message inView:(UIView *)view
{
    [self xj_dismiss];
    
    JGProgressHUD *HUD = [JGProgressHUD progressHUDWithStyle:JGProgressHUDStyleDark];
    HUD.indicatorView = nil;
    HUD.textLabel.text = message;
    HUD.position = JGProgressHUDPositionTopCenter;
    [HUD showInView:view];
    [HUD dismissAfterDelay:1.5f];
}


@end
